package spring.cloud.tasks.common;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadFactory implements java.util.concurrent.ThreadFactory {

    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String threadName;
    private boolean isMultiple = true;

    public ThreadFactory(String threadName, boolean isMultiple) {
        this.isMultiple = isMultiple;
        this.threadName = threadName;
    }

    @Override
    public Thread newThread(Runnable runnable) {
        String name = isMultiple ? threadName + "-" + threadNumber.getAndIncrement() : threadName;
        Thread thread = new Thread(runnable, name);
        thread.setDaemon(false);
        thread.setPriority(Thread.NORM_PRIORITY);
        return thread;
    }
}
